<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="showAddCateDialog()">添加分类</el-button>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <tree-table
            :data="catelist"
            :columns="columns"
            :selection-type="false"
            :expand-type="false"
            :show-index="true"
            :show-row-hover="false"
            index-text="#"
            border
          >
            <!-- slot为具名插槽 将其插入到slot名为isok的位置  -->
            <template slot="isok" slot-scope="scope">
              <i v-if="scope.row.cat_deleted===false" style="color:green" class="el-icon-success"></i>
              <i v-else style="color:red" class="el-icon-error"></i>
            </template>
            <template slot="order" slot-scope="scope">
              <el-tag v-if="scope.row.cat_level===0">一级</el-tag>
              <el-tag v-else-if="scope.row.cat_level===1" type="success">二级</el-tag>
              <el-tag v-else-if="scope.row.cat_level===2" type="warning">三级</el-tag>
            </template>
            <template slot="opt" slot-scope="scope">
              <el-button type="primary" icon="el-icon-edit">编辑</el-button>
              <el-button type="danger" icon="el-icon-delete">删除</el-button>
            </template>
          </tree-table>
          <!-- 分页 -->
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :page-sizes="[5, 11, 20, 30]"
            :page-size="querInfo.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          ></el-pagination>
        </el-col>
      </el-row>
    </el-card>
    <!-- 添加分类的对话框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addCateDialogVisible"
      @close="addCateDialogClosed"
      width="30%"
    >
      <!-- 内容主体区 -->
      <el-form
        :model="addCateForm"
        :rules="addCateFormRules"
        ref="addCateFormRef"
        label-width="100px"
      >
        <el-form-item label="分类名称:" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类:" prop="cat_name">
          <!-- option为数据源  v-model为选择的数据(必须是数组) { expandTrigger: 'hover' }-->
          <el-cascader
            expandTrigger="hover"
            v-model="selectedCatKeys"
            :options="parentCateList"
            :props="cascaderProps"
            @change="cascaderCatChange"
            clearable
            change-on-select
          ></el-cascader>
          <!-- <el-input v-model="addCateForm.cat_name"></el-input> -->
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 查询条件1.6.1
      querInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      //商品分类的数据列表，默认为空
      catelist: [],
      total: 0,
      //为table指定列明
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          //渲染的模板名
          template: 'isok'
        },
        {
          label: '排序',
          type: 'template',
          //渲染的模板名
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          //渲染的模板名
          template: 'opt'
        }
      ],
      //控制对话框的显示与隐藏
      addCateDialogVisible: false,
      //添加分类model
      addCateForm: {
        cat_name: '',
        //分类的等级，默认要添加的是1级分类
        cat_level: 0,
        //父级分类的id
        cat_pid: 0
      },
      addCateFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      //父级分类的列表
      parentCateList: [],
      //指定级联框的内容 value
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
        //   expandTrigger: 'hover',
      },
      //级联框的选择值
      selectedCatKeys: []
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.$http.get('/categories', {
        params: this.querInfo
      })
      if (res.meta.status != 200) {
        return this.$message.error('获取商品分类信息失败')
      }
      console.log(this.catelist, res.data)

      this.catelist = res.data.result
      this.total = res.data.total
    },
    //监听pagesize改变
    handleSizeChange(newSize) {

      this.querInfo.pagesize = newSize
      this.getCateList()
    },
    //监听pagenum的改变
    handleCurrentChange(newPage) {
      this.querInfo.pagenum = newPage
      this.getCateList()
    },
    //对话框关闭事件
    addCateDialogClosed() {
        this.$refs.addCateFormRef.resetFields()
        // this.$refs.addCateFormRef.setfiled
        this.selectedCatKeys=[]
        this.addCateForm.cat_level=0
        this.addCateForm.cat_pid=0
    },
    //对话框确定事件
    addCate() {
      this.$refs.addCateFormRef.validate(async value=>{
        if(!value) return
        const {data:res}=await this.$http.post('categories',
        this.addCateForm)
        if(res.meta.status!==201){
          return this.$message.error("添加分类失败")
        }
        this.$message.success('添加分类成功！')
        this.getCateList()
        this.addCateDialogVisible=false
      })
    },
    //1.6.2
    showAddCateDialog() {
      this.getParentCateList()
      this.addCateDialogVisible = true
    },
    //获取父级分类的数据列表
    async getParentCateList() {
      const { data: res } = await this.$http.get('categories', {
        params: { type: 2 }
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取父级分类数据失败')
      }
      this.parentCateList = res.data
    },
    cascaderCatChange() {
    //   console.log(this.selectedCatKeys)
      if (this.selectedCatKeys.length > 0) {
        this.addCateForm.cat_pid = this.selectedCatKeys[
          this.selectedCatKeys.length - 1
        ]
        this.addCateForm.cat_level = this.selectedCatKeys.length
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
      console.log(this.addCateForm);
      
    }
  }
}
</script>

<style lang="less" scoped>
</style>